<?php
/**
 * 商品快照
 */
namespace App;

use Model\OrderSnapshootMod;

defined('IN_IA') or exit('Access Denied');

class Snapshoot extends Action
{
    //model
    private $Snapshoot = null;

    //action
    private $GoodsAction = null;

    //field

    public function __construct()
    {
        parent::__construct();
        $this->Snapshoot = new OrderSnapshootMod();
    }

    /**
     * 创建快照
     * @param $_goods_id
     * @param $_order_id
     * @return boolean 是否创建成功
     */
    public function create($_goods_id,$_order_id)
    {
        $this->GoodsAction = new Goods();
        $this->GoodsAction->_goods_id = $_goods_id;
        $_goods = $this->GoodsAction->findGoods();
        $_goods = array_merge($this->GoodsAction->inventoryAndSales(),$_goods);
        $_data = array(
            "order_id"=>$_order_id,
            "goods_id"=>$_goods_id,
            "name"=>$_goods["name"],
            "price"=>$_goods["price"],
            "detail"=>$_goods["detail"],
            "images"=>json_encode($_goods["images"]),
            "inventory"=>$_goods["inventory"],
            "weight"=>$_goods["weight"],
            "sales"=>$_goods["sales"]
        );
        return $this->Snapshoot->insert($_data);
    }

    /**
     * 获取一条快照信息
     * @param string $_order_id 订单编号
     * @param array $_return 返回的字段
     * @return array 数据
     */
    public function findOne($_order_id,Array $_return = array())
    {
        $_snapshoot = $this->Snapshoot->findOne(
            array("order_id"=>$_order_id),
            $_return
        );
        if(isset($_snapshoot["images"])){
            $_snapshoot["images"] = json_decode($_snapshoot["images"],true);
        }
        return $_snapshoot;
    }

    /**
     * 后台订单列表获取一条快照信息
     * @param strng order_id 订单号
     * @param array return 返回的字段
     */
    public function get()
    {
        $_snapshot = $this->findOne($this->_gpc["order_id"],$this->_gpc["return"]);
        if($_snapshot){
            $_images = array();
            foreach($_snapshot["images"] as $_k=>$_v){
                $_images[] = tomedia($_v);
            }
            $_snapshot["images"] = $_images;
            $this->_result = array("status"=>"success","data"=>$_snapshot);
        }
        $this->result($this->_result);
    }
}